using MediatR;
using WMS.Domain;
using WMS.ErrorCode;
using WMS.Infrasturcture.Interfaces;
using WMS.WebApiWrite.Application.Command;

namespace WMS.WebApiWrite.Application.Commandhandler
{
    /// <summary>
    /// 处理 <see cref="DeleteStoragelocationInfoCommand"/> 删除库位信息命令的处理器
    /// </summary>
    public class DeleteStoragelocationInfoCommandHandler : IRequestHandler<DeleteStoragelocationInfoCommand, ApiResult<bool>>
    {
        private readonly IBaseRepository<StoragelocationInfo> storagelocationRepository;

        /// <summary>
        /// 构造函数，注入库位信息仓储
        /// </summary>
        /// <param name="storagelocationRepository">库位信息仓储接口</param>
        public DeleteStoragelocationInfoCommandHandler(IBaseRepository<StoragelocationInfo> storagelocationRepository)
        {
            this.storagelocationRepository = storagelocationRepository;
        }

        /// <summary>
        /// 处理 <see cref="DeleteStoragelocationInfoCommand"/> 删除库位信息命令
        /// </summary>
        /// <param name="request">命令请求</param>
        /// <param name="cancellationToken">取消令牌</param>
        /// <returns>返回操作结果</returns>
        public async Task<ApiResult<bool>> Handle(DeleteStoragelocationInfoCommand request, CancellationToken cancellationToken)
        {
            try
            {
                // 查找要删除的库位信息（只查询未删除的数据）
                var storagelocationInfo = storagelocationRepository.GetAll()
                    .Where(x => !x.IsDeleted) // 只查询未删除的记录
                    .FirstOrDefault(x => x.StoragelocationId == request.StoragelocationId);

                if (storagelocationInfo == null)
                {
                    return new ApiResult<bool>
                    {
                        Code = ApiResultEnum.删除失败,
                        Message = "库位信息不存在",
                        Data = false
                    };
                }

                // 逻辑删除：设置删除标记和删除信息
                storagelocationInfo.IsDeleted = true; // 标记为已删除
                storagelocationInfo.DeleteName = request.OperatorName;
                storagelocationInfo.DeleteTime = DateTime.Now;

                // 执行更新操作（逻辑删除，不是物理删除）
                var result = await storagelocationRepository.UpdateAsync(storagelocationInfo);

                if (result > 0)
                {
                    return new ApiResult<bool>
                    {
                        Code = ApiResultEnum.删除成功,
                        Message = "删除库位信息成功",
                        Data = true
                    };
                }
                else
                {
                    return new ApiResult<bool>
                    {
                        Code = ApiResultEnum.删除失败,
                        Message = "删除库位信息失败",
                        Data = false
                    };
                }
            }
            catch (Exception ex)
            {
                return new ApiResult<bool>
                {
                    Code = ApiResultEnum.删除失败,
                    Message = $"删除库位信息异常：{ex.Message}",
                    Data = false
                };
            }
        }
    }
}

